/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import java.sql.*;

/**
 *
 * @author Administrador
 */
public class ConectarDB {

    private Connection conn = null;

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

//-----------------------------------------------------------------------------------------------------------------------------
//METODOS DE CONEXION
//-----------------------------------------------------------------------------------------------------------------------------
    public Connection abrirConexion() {
        try {
            String userName = "root";
            String password = "admin";
            String url = "jdbc:mysql://localhost/labpaezdb";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(url, userName, password);
            System.out.println("Database connection established");
            return conn;

        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Cannot connect to database server");
//            JOptionPane.showMessageDialog(null, "Query Fallido");
            System.out.println("Query Fallido");
            return null;
        }
    }//Fin de abrirConexion

    public void cerrarConexion() {
        if (conn != null) {
            try {
                conn.close();
                System.out.println("Database connection terminated");
            } catch (Exception e) { /*
                 * ignore close errors
                 */ }
        }
    }//Fin de cerrarConexion

//-----------------------------------------------------------------------------------------------------------------------------
//METODOS DE QUERY
//-----------------------------------------------------------------------------------------------------------------------------
    public boolean ejecutarQuery(String query) {
        try {
            String userName = "root";
            String password = "admin";
            String url = "jdbc:mysql://localhost/labpaezdb";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(url, userName, password);
            System.out.println("Database connection established");

            Statement stmt = conn.createStatement();
            stmt.executeUpdate(query);
//            JOptionPane.showMessageDialog(null, "Query Exitoso");
            System.out.println("Query Exitoso");
            return true;

        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Cannot connect to database server");
//            JOptionPane.showMessageDialog(null, "Query Fallido");
            System.out.println("Query Fallido");
            return false;
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                    System.out.println("Database connection terminated");
                } catch (Exception e) { /*
                     * ignore close errors
                     */ }
            }
        }
    }//Fin de ejecutarQuery

    public ResultSet ejecutarQueryRS(String query) {
        ResultSet rs = null;
        try {
            String userName = "root";
            String password = "admin";
            String url = "jdbc:mysql://localhost/labpaezdb";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(url, userName, password);
            System.out.println("Database connection established");

            Statement stmt = conn.createStatement();
            rs = stmt.executeQuery(query);

//            JOptionPane.showMessageDialog(null, "Query RS Exitoso");
            System.out.println("Query Exitoso");
        } catch (Exception e) {
            System.out.println(e);
            System.err.println("Cannot connect to database server");
//            JOptionPane.showMessageDialog(null, "Query RS Fallido");
            System.out.println("Query Fallido");
        }
        return rs;
    }//Fin de ejecutarQueryRS
}//Fin de class